package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.TradeArea;
import com.chen.dto.R;
import com.chen.service.TradeAreaService;
import com.chen.utils.UserUtil;
import com.chen.vo.TradeAreaMarketVo;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: chen
 * @Date: 2021/5/17 9:50 AM
 * @Version 1.0
 */
@RestController
@Api(tags = "交易区域相关控制器")
@RequestMapping("/tradeAreas")
public class TradeAreasController {

    @Autowired
    private TradeAreaService tradeAreaService;

    @GetMapping("/markets")
    public R<List<TradeAreaMarketVo>> markets(){
        List<TradeAreaMarketVo> list = tradeAreaService.getMarkets(null);
        return R.ok(list);
    }

    @GetMapping("/market/favorite")
    public R<List<TradeAreaMarketVo>> favorite(){
        List<TradeAreaMarketVo> list = tradeAreaService.getMarkets(UserUtil.getUser());
        return R.ok(list);
    }



    @GetMapping("/all")
    public R<List<TradeArea>> all(){
        List<TradeArea> list = tradeAreaService.list();
        return R.ok(list);
    }

    @GetMapping
    public R<Page<TradeArea>> list(Page<TradeArea> page,String name,Integer status){

        Page<TradeArea> list = tradeAreaService.page(page,
                new LambdaQueryWrapper<TradeArea>().
                like(StringUtils.isNotBlank(name), TradeArea::getName, name).
                eq(status != null, TradeArea::getStatus, status).
                orderByDesc(TradeArea::getCreated));

        return R.ok(list);
    }

    @PostMapping
    public R save(@RequestBody TradeArea tradeArea){
        tradeAreaService.save(tradeArea);
        return R.ok();
    }

    @PatchMapping
    public R update(@RequestBody TradeArea tradeArea){
        tradeAreaService.updateById(tradeArea);
        return R.ok();
    }

    @PostMapping("/status")
    public R status(@RequestBody TradeArea tradeArea){
        tradeAreaService.updateById(tradeArea);
        return R.ok();
    }

}
